package com.study.hot.jetpack.databinding.recyclerview;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.study.hot.R;
import com.study.hot.databinding.ItemUserBinding;
import com.study.hot.jetpack.databinding.model.User;

import java.util.List;

/**
 * <pre>
 *     author : fauks
 *     time   : 2020/03/25
 *     desc   :
 * </pre>
 */
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    private List<User> userList;

    public UserAdapter(List<User> userList) {
        this.userList = userList;
    }

    @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemUserBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_user, parent, false);
        return new ViewHolder(binding);
    }

    @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.getBinding().setUser(userList.get(position));
    }

    @Override public int getItemCount() {
        return userList.size();
    }

    //使用MVVM
    static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemUserBinding binding;

        public ViewHolder(@NonNull ItemUserBinding userBinding) {
            super(userBinding.getRoot());
            this.binding = userBinding;
        }

        public ItemUserBinding getBinding() {
            return binding;
        }
    }
}
